import request from '../request.js';
import userService from '@/service/user.js';
import $store from '@/store/index.js';
// #ifdef H5
const cache_time = (process.env.NODE_ENV !== 'development')?72000:30;
// #endif
// #ifndef H5
const cache_time = 30;
// #endif

module.exports = {
	
	image(title,imageUrl,scene='friend'){
		uni.showLoading({
			mask: 'true',
			title: '加载中～'
		});	
		if (!imageUrl){
			imageUrl = title.imageUrl;
			scene = title.scene;
			title = title.title;
		}
		return new Promise((resolve, reject) => {
			let wxScene='WXSceneTimeline';
			if (scene=='friend'){
				wxScene='WXSceneSession';
			}else if (scene=='favorite'){
				wxScene='WXSceneFavorite';
			}else if (scene=='timeline'){
				wxScene='WXSceneTimeline';		
			}
			uni.share({
				provider: "weixin",
				scene: wxScene,
				type: 2,
				title: title,
				imageUrl: imageUrl,
				success: function(res) {
					res.process = true;
					resolve(res)
					if (res.process){
						uni.showToast({
							title: '分享成功！',
							icon: 'none'
						})
					}
					
				},
				fail: function(err) {
					err.process = true;
					reject(err);
					if (err.process){
						uni.showToast({
							title: '分享失败！',
							icon: 'none'
						})
					}
					console.log("fail:" + JSON.stringify(err));
					
				},
				complete(err) {
					uni.hideLoading();
					reject(err);
				},
			});
		})

	
	},
 

	app(title,imageUrl=false,miniProgram=false){
		uni.showLoading({
			mask: 'true',
			title: '加载中～'
		});	
		if (!imageUrl && !miniProgram){
			imageUrl = title.imageUrl;
			miniProgram = title.miniProgram;
			title = title.title;
		}
		return new Promise((resolve, reject) => {
			uni.share({
				provider: "weixin",
				scene: "WXSceneSession",
				type: 5,
				title: title,
				imageUrl: imageUrl,
				miniProgram: miniProgram,
				success: function(res) {
					res.process = true;
					resolve(res)
					if (res.process){
						uni.showToast({
							title: '分享成功！',
							icon: 'none'
						})
					}
				},
				fail: function(err) {
					console.log("fail:" + JSON.stringify(err));
					err.process = true;
					reject(err);
					if (err.process){
						uni.showToast({
							title: '分享失败！',
							icon: 'none'
						})
					}
				},
				complete(err) {
					uni.hideLoading();
					reject(err);
				},
			});
		})
 
	},
	mp(data,scene='friend'){
		uni.showLoading({
			mask: 'true',
			title: '加载中～'
		});	
		return new Promise((resolve, reject) => {
			let wxScene='WXSceneTimeline';
			if (scene=='friend'){
				wxScene='WXSceneSession';
			}else if (scene=='favorite'){
				wxScene='WXSceneFavorite';
			}else if (scene=='timeline'){
				wxScene='WXSceneTimeline';		
			}
			// #ifdef MP-WEIXIN
			uni.navigateToMiniProgram({
			  appId: data.miniProgram.appId,
			  path: data.miniProgram.path,
			  extraData:data.extra?data.extra:{},
			  success(res) {
			    // 打开成功
				res.process = true;
				resolve(res)
				if (res.process){
					uni.showToast({
						title: '分享成功！',
						icon: 'none'
					})
				}
			  }
			});
			// #endif
			// #ifndef MP-WEIXIN
			uni.share({
				provider: "weixin",
				scene: wxScene,
				type: 5,
				title: data.title,
				imageUrl: data.imageUrl,
				miniProgram: data.miniProgram,
				success: function(res) {
					res.process = true;
					resolve(res)
					if (res.process){
						uni.showToast({
							title: '分享成功！',
							icon: 'none'
						})
					}
				},
				fail: function(err) {
					console.log("fail:" + JSON.stringify(err));
					err.process = true;
					reject(err);
					if (err.process){
						uni.showToast({
							title: '分享失败！',
							icon: 'none'
						})
					}
				},
				complete(err) {
					uni.hideLoading();
					//reject(err);
				},
			});
			// #endif
		})
	 
	}
}
